﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LightManager.UserControls.UnitEditor
{
    public partial class UnitEditorControl : UserControl
    {
        private Unit subject;

        public event EventHandler<ValidationEventArgs> NameValidation;

        public UnitEditorControl()
        {
            InitializeComponent();
            this.Initialize();
        }

        private void Initialize()
        {
            this.Unit = new Unit();
            List<NumericUpDown> controls = new List<NumericUpDown>();
            controls.AddRange(this.flowLayoutPanel.Controls.OfType<NumericUpDown>());
            controls.AddRange(this.flowLayoutPanel1.Controls.OfType<NumericUpDown>());
            controls.AddRange(this.flowLayoutPanel2.Controls.OfType<NumericUpDown>());
            foreach (DamageSources source in Enum.GetValues(typeof(DamageSources)))
                foreach (Control c in controls)
                    if (c.Name.Contains(source.ToString()))
                        this.toolTip.SetToolTip(c, source.ToString());

            this.errorProvider.SetIconAlignment(this.txtName, ErrorIconAlignment.MiddleRight);
        }

        [Browsable(false)]
        public Unit Unit
        {
            set
            {
                this.subject = value;
                this.txtName.Text = this.subject.Name;
                this.txtPlayer.Text = this.subject.PlayerName;
                this.nudLife.Value = (this.subject.LifeStatus.Count > 0) ? this.subject.LifeStatus.Count : 20;
                this.chkIsEnemy.Checked = this.subject.IsEnemy;

                this.nudArmorAir.Value = this.subject.Armors[DamageSources.Air];
                this.nudArmorChange.Value = this.subject.Armors[DamageSources.Change];
                this.nudArmorDeath.Value = this.subject.Armors[DamageSources.Death];
                this.nudArmorEarth.Value = this.subject.Armors[DamageSources.Earth];
                this.nudArmorEternal.Value = this.subject.Armors[DamageSources.Eternal];
                this.nudArmorFire.Value = this.subject.Armors[DamageSources.Fire];
                this.nudArmorLife.Value = this.subject.Armors[DamageSources.Life];
                this.nudArmorPhisical.Value = this.subject.Armors[DamageSources.Phisical];
                this.nudArmorWater.Value = this.subject.Armors[DamageSources.Water];

                this.nudResAir.Value = this.subject.Resistances[DamageSources.Air];
                this.nudResChange.Value = this.subject.Resistances[DamageSources.Change];
                this.nudResDeath.Value = this.subject.Resistances[DamageSources.Death];
                this.nudResEarth.Value = this.subject.Resistances[DamageSources.Earth];
                this.nudResEternal.Value = this.subject.Resistances[DamageSources.Eternal];
                this.nudResFire.Value = this.subject.Resistances[DamageSources.Fire];
                this.nudResLife.Value = this.subject.Resistances[DamageSources.Life];
                this.nudResWater.Value = this.subject.Resistances[DamageSources.Water];

                this.nudShieldPhisical.Value = this.subject.Shields[DamageSources.Phisical];
                this.nudShieldAir.Value = this.subject.Shields[DamageSources.Air];
                this.nudShieldChange.Value = this.subject.Shields[DamageSources.Change];
                this.nudShieldDeath.Value = this.subject.Shields[DamageSources.Death];
                this.nudShieldEarth.Value = this.subject.Shields[DamageSources.Earth];
                this.nudShieldEternal.Value = this.subject.Shields[DamageSources.Eternal];
                this.nudShieldFire.Value = this.subject.Shields[DamageSources.Fire];
                this.nudShieldLife.Value = this.subject.Shields[DamageSources.Life];
                this.nudShieldWater.Value = this.subject.Shields[DamageSources.Water];
                this.nudShieldEthereal.Value = this.subject.Shields[DamageSources.Ethereal];
            }
            get { return this.subject; }
        }

        public Unit ApplyChanges()
        {
            this.subject.Name = this.txtName.Text;
            this.subject.PlayerName = this.txtPlayer.Text;
            this.subject.Initialize(Convert.ToInt32(this.nudLife.Value));
            this.subject.IsEnemy = this.chkIsEnemy.Checked;

            this.subject.Armors[DamageSources.Air] = Convert.ToInt32(this.nudArmorAir.Value);
            this.subject.Armors[DamageSources.Change] = Convert.ToInt32(this.nudArmorChange.Value);
            this.subject.Armors[DamageSources.Death] = Convert.ToInt32(this.nudArmorDeath.Value);
            this.subject.Armors[DamageSources.Earth] = Convert.ToInt32(this.nudArmorEarth.Value);
            this.subject.Armors[DamageSources.Eternal] = Convert.ToInt32(this.nudArmorEternal.Value);
            this.subject.Armors[DamageSources.Fire] = Convert.ToInt32(this.nudArmorFire.Value);
            this.subject.Armors[DamageSources.Life] = Convert.ToInt32(this.nudArmorLife.Value);
            this.subject.Armors[DamageSources.Phisical] = Convert.ToInt32(this.nudArmorPhisical.Value);
            this.subject.Armors[DamageSources.Water] = Convert.ToInt32(this.nudArmorWater.Value);

            this.subject.Resistances[DamageSources.Air] = Convert.ToInt32(this.nudResAir.Value);
            this.subject.Resistances[DamageSources.Change] = Convert.ToInt32(this.nudResChange.Value);
            this.subject.Resistances[DamageSources.Death] = Convert.ToInt32(this.nudResDeath.Value);
            this.subject.Resistances[DamageSources.Earth] = Convert.ToInt32(this.nudResEarth.Value);
            this.subject.Resistances[DamageSources.Eternal] = Convert.ToInt32(this.nudResEternal.Value);
            this.subject.Resistances[DamageSources.Fire] = Convert.ToInt32(this.nudResFire.Value);
            this.subject.Resistances[DamageSources.Life] = Convert.ToInt32(this.nudResLife.Value);
            this.subject.Resistances[DamageSources.Water] = Convert.ToInt32(this.nudResWater.Value);

            this.subject.Shields[DamageSources.Phisical] = Convert.ToInt32(this.nudShieldPhisical.Value);
            this.subject.Shields[DamageSources.Air] = Convert.ToInt32(this.nudShieldAir.Value);
            this.subject.Shields[DamageSources.Change] = Convert.ToInt32(this.nudShieldChange.Value);
            this.subject.Shields[DamageSources.Death] = Convert.ToInt32(this.nudShieldDeath.Value);
            this.subject.Shields[DamageSources.Earth] = Convert.ToInt32(this.nudShieldEarth.Value);
            this.subject.Shields[DamageSources.Eternal] = Convert.ToInt32(this.nudShieldEternal.Value);
            this.subject.Shields[DamageSources.Fire] = Convert.ToInt32(this.nudShieldFire.Value);
            this.subject.Shields[DamageSources.Life] = Convert.ToInt32(this.nudShieldLife.Value);
            this.subject.Shields[DamageSources.Water] = Convert.ToInt32(this.nudShieldWater.Value);
            this.subject.Shields[DamageSources.Ethereal] = Convert.ToInt32(this.nudShieldEthereal.Value);

            return this.subject;
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            if (this.NameValidation == null) return;
            ValidationEventArgs args = new ValidationEventArgs();
            args.Value = this.txtName.Text;
            this.NameValidation(this.subject, args);
            this.errorProvider.SetError(this.txtName, (args.IsValid) ? "" : "Nome già utilizzato o contenente spazi");
        }

        public bool HaveErrors
        {
            get { return !string.IsNullOrEmpty(this.errorProvider.GetError(this.txtName)); }
        }
    }
}
